package com.bank.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bank.code.RespCode;
import com.bank.dao.SubjectMapper;
import com.bank.entity.Subject;
import com.bank.exception.CommonException;
import com.bank.service.SubjectService;

@Service(value="subjectService")
public class SubjectServiceImpl implements SubjectService {

	
	@Autowired
	SubjectMapper subjectMapper;

	/**
	 * 
	 * 保存科目信息
	 * @param params 科目信息参数
	 * @return
	 * 
	 */
	public void save(Map<String, Object> params) throws CommonException {
		String subjectNumber = (String)params.get("subject_number");
		Subject subject = subjectMapper.selectByPrimaryKey(subjectNumber);
		if(subject != null){
			throw new CommonException(RespCode.SUB_IS_EXIST, "科目已存在");
		}
		subject = new Subject();
		subject.setSubject_number(subjectNumber);
		subject.setSubject_name((String)params.get("subject_name"));
		subject.setSubject_balance_direct((String)params.get("subject_balance_direct"));
		subject.setSubject_type((String)params.get("subject_type"));
		subject.setSubject_level((String)params.get("subject_level"));
		subject.setSuper_subject((String)params.get("super_subject"));
		subject.setSubject_status("1");
		subjectMapper.insertSelective(subject);
	}

	/**
	 * 更新科目信息
	 * 
	 */
	public void update(Map<String, Object> parmas) throws CommonException {
		String subjectNumber = (String)parmas.get("subject_number");
		Subject subject = subjectMapper.selectByPrimaryKey(subjectNumber); 
		if(subject == null){
			throw new CommonException(RespCode.SUB_NOT_EXIST, "科目不存在");
		}
		subject.setSubject_name((String)parmas.get("subject_name"));
		subject.setSubject_balance_direct((String)parmas.get("subject_balance_direct"));
		subject.setSubject_type((String)parmas.get("subject_type"));
		subject.setSubject_level((String)parmas.get("subject_level"));
		subject.setSuper_subject((String)parmas.get("super_subject"));
		
		subject.setSubject_status("1");
		subjectMapper.updateByPrimaryKey(subject);
	}
	
	/**
	 * 
	 * 根据科目号查询科目信息
	 * 
	 */
	public Subject get(Map<String, Object> parmas) {
		return subjectMapper.selectByPrimaryKey((String)parmas.get("subject_number"));
	}
	
	/**
	 * 获取科目信息列表
	 * 
	 */
	public List<Object> getSubjectList(Map<String, Object> param) {
		List<Subject> subjectList = subjectMapper.selectSubjectList(param);
		List<Object> objectList = new ArrayList<Object>();
		for(Subject subject:subjectList){
			objectList.add(subject);
		}
		return objectList;

	}

	
	
	
}
